<template>
	<view class="conversation">
		<template v-if="list.length">
			<view
				@click="clickMoreButton"
				class="more-content">
				{{listPageText}}
			</view>
			<template v-for="item in list">
				<view class="for" v-if="item.user.uid == userUid">
					<view
						class="_avatar"
						@click="$_to('user/detail?uid=' + item.user.uid)">
						<image :src="item.user.avatar" mode="widthFix"></image>
					</view>
					<view class="_content">
						<view class="_content-top">
							<text class="_nickname">{{item.user.nickname}}</text>
							<text class="_editedTimeAgo">{{item.timeAgo}}</text>
						</view>
						<view class="_content-bottom">
							<view class="_content-box" v-if="item.content">{{item.content}}</view>
							<view
								class="_content-box-file"
								v-if="item.file">
								<global-audio
									v-if="item.file.audioUrl"
									Css="width:400rpx;"
									:Url="item.file.audioUrl"></global-audio>
								<view
									class="_content-box-file-image"
									v-if="item.file.imageBigUrl"
									@click="clickImageButton(item.file.imageBigUrl)">
									<image :src="item.file.imageConfigUrl" mode="widthFix"></image>
								</view>
								<view
									class="_content-box-file-video"
									v-if="item.file.videoUrl"
									@click="$_to('pages/video?url=' + encodeURIComponent(item.file.videoUrl))">
									<text class="iconfont icon-arrow-right-filling"></text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="for _right" v-else>
					<view class="_content">
						<view class="_content-top">
							<text class="_editedTimeAgo">{{item.timeAgo}}</text>
							<text class="_nickname">{{item.user.nickname}}</text>
						</view>
						<view class="_content-bottom">
							<view class="_content-box" v-if="item.content">{{item.content}}</view>
							<view
								class="_content-box-file"
								v-if="item.file">
								<global-audio
									v-if="item.file.audioUrl"
									Css="width:400rpx;"
									:Url="item.file.audioUrl"></global-audio>
								<view
									class="_content-box-file-image"
									v-if="item.file.imageBigUrl"
									@click="clickImageButton(item.file.imageBigUrl)">
									<image :src="item.file.imageConfigUrl" mode="widthFix"></image>
								</view>
								<view
									class="_content-box-file-video"
									v-if="item.file.videoUrl"
									@click="$_to('pages/video?url=' + encodeURIComponent(item.file.videoUrl))">
									<text class="iconfont icon-arrow-right-filling"></text>
								</view>
							</view>
						</view>
					</view>
					<view class="_avatar">
						<image :src="item.user.avatar" mode="widthFix"></image>
					</view>
				</view>
			</template>
		</template>
		<template v-else>
			<global-loading></global-loading>
		</template>
		<template v-if="editorConfig">
			<box-comment-fixed :editorConfig="editorConfig"></box-comment-fixed>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userUid:"",
				list:[],
				content:"",
				listPage:2,
				listPageText:this.$t('clickToLoadMore'),
				editorConfig:null
			}
		},
		onLoad(e) {
			uni.$on('boxCommentFixedContent',(data) => {
				uni.showLoading();
				this.content = data.msg;
				this.postConversationMessage();
			})
			uni.$on('boxCommentFixedContentFile',(data) => {
				uni.showLoading();
				this.postConversationMessageFile(data.msg,data.type);
			})
			this.userUid = e.uid;
			// 更新阅读状态
			this.$_request(
				"PATCH",`/api/fresns/v1/conversation/${this.userUid}/read-status`,{},
				(res) => {}
			)
			this.getConversationMessages();
			this.getConversationDdetail();
		},
		onUnload() {
			uni.$off('boxCommentFixedContent');
			uni.$off('boxCommentFixedContentFile');
		},
		methods: {
			// 获取对话详情
			getConversationDdetail(){
				this.$_request(
					"GET",`/api/fresns/v1/conversation/${this.userUid}/detail`,{},
					(res) => {
						this.editorConfig = res.data.data.configs;
					}
				)
			},
			// 获取对话列表
			getConversationMessages(){
				this.$_request(
					"GET",`/api/fresns/v1/conversation/${this.userUid}/messages`,
					{
						"orderDirection":"desc",
						"pageListDirection":"oldest",
						"filterUserType":"whitelist",
						"filterUserKeys":"fsid,uid,nickname,avatar",
						"pageSize":15,
						"page":1
					},
					(res) => {
						this.list = res.data.data.list;
						this.$nextTick(() => {
							uni.pageScrollTo({
								scrollTop: 99999999,
								duration: 88
							});
						});
						console.log(this.list)
					}
				)
			},
			// 发布对话
			postConversationMessage(){
				this.$_request(
					"POST","/api/fresns/v1/conversation/message",
					{
						"uidOrUsername":this.userUid,
						"message":this.content
					},
					(res) => {
						uni.hideLoading();
						if(res.data.code === 0){
							this.getConversationMessages();
						}else{
							uni.showToast({
								title: res.data.message,
								duration: 1500
							});
						}
					}
				)
			},
			// 加载更多
			clickMoreButton(){
				if (this.listPageText == this.$t('loading')) { return }
				this.listPageText = this.$t('loading');
				this.$_request(
					"GET",`/api/fresns/v1/conversation/${this.userUid}/messages`,
					{
						"orderDirection":"desc",
						"pageListDirection":"oldest",
						"filterUserType":"whitelist",
						"filterUserKeys":"fsid,uid,nickname,avatar",
						"pageSize":15,
						"page": this.listPage
					},
					(res) => {
						if (res.data.data.list.length) {
							this.list = res.data.data.list.concat(this.list)
							this.listPage++
							this.listPageText = this.$t('clickToLoadMore')
						} else {
							this.listPageText = this.$t('listEmpty')
						}
					}
				)
			},
			clickImageButton(url){
				uni.previewImage({
					current: 0,
					urls: [url]
				})
			},
			postConversationMessageFile(url,type){
				uni.hideLoading();
				const _this = this;
				uni.uploadFile({
					url: _this.$_url() + "/api/fresns/v1/common/file/upload",
					filePath: url,
					name: "file",
					header: _this.$_fresnsHeader(),
					formData:{
						"usageType":"conversation",
						"usageFsid":_this.userUid,
						"type":type
					},
					success(res) {
						if(JSON.parse(res.data).code == 0){
							_this.$_request(
								"POST","/api/fresns/v1/conversation/message",
								{
									"uidOrUsername":_this.userUid,
									"fid":JSON.parse(res.data).data.fid
								},
								(ref) => {
									uni.hideLoading();
									if(ref.data.code === 0){
										_this.getConversationMessages();
									}else{
										uni.showToast({
											title: ref.data.message,
											duration: 1500
										});
									}
								}
							)
							
							
						}else{
							uni.showToast({
								title: JSON.parse(res.data).message,
								icon: "none",
								duration: 1000
							});
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
.conversation{
	padding: 0 25rpx;
	.for{
		display: flex;
		padding-top: 25rpx;
		._avatar{
			width: 80rpx;
			height: 80rpx;
			flex-shrink: 0;
			border-radius: 5rpx;
			overflow: hidden;
		}
		._content{
			flex: 1;
			margin-left: 15rpx;
			._content-top{
				display: flex;
				align-items: center;
				line-height: 22rpx;
				padding: 5rpx 0 10rpx 0;
				text{
					font-size: 22rpx;
					padding-right: 10rpx;
				}
				._editedTimeAgo{
					color: $placeholder;
				}
			}
			._content-bottom{
				._content-box{
					display: inline-block;
					background: #FFF;
					padding: 15rpx 20rpx;
					border-radius: 10rpx;
				}
				._content-box-file{
					display: inline-block;
					._content-box-file-image{
						width: 500rpx;
						background: #FFF;
						padding: 10rpx;
					}
					._content-box-file-video{
						width: 200rpx;
						height: 200rpx;
						background: #000;
						display: flex;
						align-items: center;
						justify-content: center;
						text{
							color: #FFF;
							font-size: 70rpx;
						}
					}
				}
			}
		}
	}
	._right{
		._content{
			margin-right: 15rpx;
			._content-top{
				justify-content: flex-end;
			}
			._content-bottom{
				display: flex;
				justify-content: flex-end;
				._content-box{
					display: inline-block;
					background: $green;
					padding: 15rpx 20rpx;
					border-radius: 10rpx;
				}
			}
		}
	}
	.more-content{
		padding: 25rpx 0;
		font-size: 22rpx;
		line-height: 22rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: $placeholder;
	}
}
</style>